﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SISLOG.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.Unity;
using System.Data.Common;
using System.Data;

namespace SISLOG.DataAccess
{
    public class DetalleOrdenCompraDAO : MasterDAEL, IDetalleOrdenCompraDAO
    {
        [Dependency]
        public IMaterialDAO MaterialDAO { get; set; }

        public List<DetalleOrdenCompra> GetFromDetalleOrdenCompraByOrdenId(int idOrdenCompra)
        {
            var rowMapper = MapBuilder<DetalleOrdenCompra>
                            .MapAllProperties()
                            .DoNotMap(p => p.CodigoLabel)
                            .Map(p => p.Material)
                            .WithFunc(p => MaterialDAO.GetAllFromMaterialByIdMaterial(p.GetInt32(p.GetOrdinal("idMaterial"))))
                            .Build();

            var query = dbSisLog.ExecuteSprocAccessor<DetalleOrdenCompra>("GetFromDetalleOrdenCompraByOrdenId", rowMapper, idOrdenCompra);

            return query.ToList();
        }

        public void RegistrarDetalleOrdenCompra(List<DetalleOrdenCompra> detalle, int idOrdenCompra)
        {
            foreach (var item in detalle)
            {
                item.idOrdenCompra = idOrdenCompra;
                RegistrarDetalleOrdenCompra(item);
            }
        }

        public void RegistrarDetalleOrdenCompra(DetalleOrdenCompra detalle)
        {
            DbCommand command = dbSisLog.GetStoredProcCommand("RegistrarDetalleOrdenCompra");

            dbSisLog.AddInParameter(command, "item", DbType.Int32, detalle.idDetalleOrdCompra);
            dbSisLog.AddInParameter(command, "idMaterial", DbType.Int32, detalle.idMaterial);
            dbSisLog.AddInParameter(command, "idOrdenCompra", DbType.Int32, detalle.idOrdenCompra);
            dbSisLog.AddInParameter(command, "descripcion", DbType.String, detalle.descripcion);
            dbSisLog.AddInParameter(command, "cantidad", DbType.Int32, detalle.cantidad);

            dbSisLog.ExecuteNonQuery(command);
        }

        public void EliminarDetalle(int idDetalleOrdCompra)
        {
            throw new NotImplementedException();
        }        
    }   
}
